<template>
  <div class="dashboard-container">
    <div class="upbox">
      <div class="upleftbox">
        <el-row class="upleftup">
          <el-col :span="13">
            <div class="tasknum">
              <div class="header">
                <div class="title">
                  工单统计<span class="sub-title">
                    {{ fifteenDay }}
                  </span>
                </div>
              </div>
              <div class="body">
                <div class="stats">
                  <div class="item">
                    <div class="numcolor">{{ totaltask }}</div>
                    <div class="textcolor">工单总数（个）</div>
                  </div>
                </div>
                <div class="stats">
                  <div class="item">
                    <div class="numcolor">{{ completedtask }}</div>
                    <div class="textcolor">完成工单（个）</div>
                  </div>
                </div>
                <div class="stats">
                  <div class="item">
                    <div class="numcolor">{{ progresstask }}</div>
                    <div class="textcolor">进行工单（个）</div>
                  </div>
                </div>
                <div class="stats">
                  <div class="item">
                    <div class="numcolor">{{ canceltask }}</div>
                    <div class="textcolor">取消工单（个）</div>
                  </div>
                </div>
              </div>
            </div>
          </el-col>
          <el-col :span="11">
            <div class="sellnum">
              <div class="header">
                <div class="title">
                  销售统计<span class="sub-title">
                    {{ fifteenDay }}
                  </span>
                </div>
              </div>
              <div class="body">
                <div class="stats">
                  <div class="item">
                    <div class="numcolor">{{ OrderCount }}</div>
                    <div class="textcolor">订单量（个）</div>
                  </div>
                </div>
                <div class="stats">
                  <div class="item">
                    <div class="numcolor">{{ OrderAmount }}</div>
                    <div class="textcolor">销售额（万元）</div>
                  </div>
                </div>
              </div>
            </div>
          </el-col>
        </el-row>
        <el-row class="upleftdown">
          <div class="header">
            <div class="title">
              销售数据<span class="sub-title"> {{ fifteenDay1 }} </span>
            </div>
            <div class="title-right">
              <span
                @click="changetimerw('zhou')"
                :class="{ active: timer === 'zhou' }"
                >周</span
              >
              <span
                @click="changetimerm('yue')"
                :class="{ active: timer === 'yue' }"
                >月</span
              >
              <span
                @click="changetimery('nian')"
                :class="{ active: timer === 'nian' }"
                >年</span
              >
            </div>
          </div>
          <div class="body">
            <div ref="chart" style="width: 50%; height: 250px"></div>
            <div ref="chart1" style="width: 50%; height: 250px"></div>
          </div>
        </el-row>
      </div>
      <div class="uprightbox">
        <div class="salerank">
          <div class="header">
            <div class="title">
              商品热榜<span class="sub-title"> {{ fifteenDay }} </span>
            </div>
          </div>
          <div class="body">
            <el-row
              v-for="(item, index) in toptenlist"
              :key="index"
              type="flex"
              align="middle"
              justify="space-between"
            >
              <el-col :span="5"
                ><div class="top">{{ index + 1 }}</div></el-col
              >
              <el-col :span="13"
                ><div class="sku-name">{{ item.skuName }}</div></el-col
              >
              <el-col :span="6"
                ><div class="count">{{ item.count }}单</div></el-col
              >
            </el-row>
          </div>
        </div>
      </div>
    </div>
    <div class="downbox">
      <el-row type="flex" justify="space-between">
        <el-col :span="16" class="partner">
          <div class="header">
            <div class="title">合作商点位数Top5</div>
            <div class="title-right">
              <i class="el-icon-chat-dot-round"></i>
            </div>
          </div>
          <div class="body">
            <div class="pie">
              <div ref="chart2" style="width: 300px; height: 250px"></div>
            </div>
            <div class="table">
              <div class="stats">
                <div class="item">
                  <div class="numcolor">{{ nodecount }}</div>
                  <div class="textcolor">点位数</div>
                </div>
                <div class="item">
                  <div class="numcolor">{{ partnercount }}</div>
                  <div class="textcolor">合作商数</div>
                </div>
              </div>
            </div>
          </div>
        </el-col>
        <el-col :span="8" class="vm">
          <div class="header">
            <div class="title">异常设备监控</div>
            <div class="title-right">
              <i class="el-icon-chat-dot-round"></i>
            </div>
          </div>
          <div class="body">
            <img
              src="http://likede2-admin.itheima.net/img/empty.87c4f71b.png"
              alt=""
            />
            <span>暂无数据</span>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import {
  getAllTaskNum,
  getOrderCount,
  getOrderAmount,
  getTopTen,
  getLineInfo,
  getBarInfo,
  getPieInfo,
  getNodeCount,
  getPartnerCount,
} from "@/api/task";
import { mapGetters } from "vuex";
import store from "@/store";

export default {
  name: "Dashboard",
  components: {},
  data() {
    return {
      timer: "zhou",
      start: "",
      end: "",
      start1: "",
      end1: "",
      start2: "",
      end2: "",
      OrderAmount: "",
      OrderCount: "",
      start3: "",
      end3: "",
      nodecount: "",
      partnercount: "",
      tasknum: [],
      toptenlist: [],
      lineSS: [],
      lineXX: [],
      barSS: [],
      barXX: [],
      pielist: [],
    };
  },
  computed: {
    ...mapGetters(["name"]),
    fifteenDay() {
      return this.start + "~" + this.end;
    },
    fifteenDay1() {
      return this.start3 + "~" + this.end3;
    },
    totaltask() {
      return this.tasknum.reduce((sum, item) => {
        return (sum += item.total);
      }, 0);
    },
    canceltask() {
      return this.tasknum.reduce((sum, item) => {
        return (sum += item.cancelTotal);
      }, 0);
    },
    completedtask() {
      return this.tasknum.reduce((sum, item) => {
        return (sum += item.completedTotal);
      }, 0);
    },
    progresstask() {
      return this.tasknum.reduce((sum, item) => {
        return (sum += item.progressTotal);
      }, 0);
    },
  },
  async created() {
    await store.dispatch("user/getUserInfo");
    this.start = this.$moment().subtract("15", "d").format("YYYY.MM.DD"); //当前时间往前推1天（24小时）：2021-05-10 18:42:53
    this.end = this.$moment().format("YYYY.MM.DD");

    this.start1 = this.$moment()
      .subtract("15", "d")
      .format("YYYY-MM-DD 00:00:00");
    this.end1 = this.$moment().format("YYYY-MM-DD 23:59:59");
    const res = await getAllTaskNum(this.start1, this.end1);
    this.tasknum = res;

    this.start2 = this.$moment()
      .subtract("15", "d")
      .format("YYYY-MM-DD+00:00:00");
    this.end2 = this.$moment().format("YYYY-MM-DD+23:59:59");

    const res1 = await getOrderAmount(this.start2, this.end2);
    const res2 = await getOrderCount(this.start2, this.end2);
    this.OrderAmount = (res1 / 1000000).toFixed(2);
    this.OrderCount = res2;

    this.start3 = this.$moment().subtract("15", "d").format("YYYY-MM-DD");
    this.end3 = this.$moment().format("YYYY-MM-DD");

    const res3 = await getTopTen(10, this.start3, this.end3);
    this.toptenlist = res3;

    this.getLinetbale(1);
    this.getBartable();

    const res4 = await getPieInfo();
    const all = res4.reduce((sum, item) => {
      return (sum += item.value);
    }, 0);
    const res41 = res4.map((item) => {
      return { value: (item.value / all).toFixed(1) * 100, name: item.name };
    });
    this.pielist = res41;

    const res5 = await getNodeCount();
    this.nodecount = res5;
    const res6 = await getPartnerCount();
    this.partnercount = res6;

  },
  mounted() {
    setTimeout(() => {
      this.getEchartData();
      this.getEchartData1();
      this.getEchartData2();
    }, 1500);
  },
  methods: {
    async getLinetbale(type = 1) {
      const res7 = await getLineInfo(type, this.start3, this.end3);
      this.lineSS = res7.series.map((item) => {
        return item / 10000;
      });
      this.lineXX = res7.xAxis;
    },
    async getBartable() {
      const res8 = await getBarInfo(this.start3, this.end3);
      this.barSS = res8.series.map((item) => {
        return item / 10000;
      });
      this.barXX = res8.xAxis;
    },
    changetimerw(a) {
      this.timer = a;
      this.start3 = this.$moment().startOf('isoWeek').format('YYYY-MM-DD');
      this.end3 = this.$moment().format("YYYY-MM-DD");
      this.getLinetbale(1);
      this.getBartable();
      setTimeout(() => {
        this.getEchartData();
        this.getEchartData1();
      }, 500);
    },
    changetimerm(a) {
      this.timer = a;
      this.start3 = this.$moment().subtract("15", "d").format("YYYY-MM-DD");
      this.end3 = this.$moment().format("YYYY-MM-DD");
      this.getLinetbale(1);
      this.getBartable();
      setTimeout(() => {
        this.getEchartData();
        this.getEchartData1();
        this.getEchartData2();
      }, 500);
    },
    changetimery(a) {
      this.timer = a;
      this.start3 = this.$moment().format("YYYY-01-01");
      this.end3 = this.$moment().format("YYYY-MM-DD");
      this.getLinetbale(2);
      this.getBartable();
      setTimeout(() => {
        this.getEchartData();
        this.getEchartData1();
      }, 500);
    },
    getEchartData() {
      const chart = this.$refs.chart;
      if (chart) {
        const myChart = this.$echarts.init(chart);
        const option = {
          title: {
            text: "销售额趋势图",
            top: "2%",
            left: "42%",
          },
          xAxis: {
            type: "category",
            data: this.lineXX,
            axisLine: {
              lineStyle: {
                type: "dashed",
                color: "#d3d3d3",
              },
            },
            axisLabel: {
              color: "#000",
            },
          },
          tooltip: {
            trigger: "axis",
            axisPointer: {
              // 坐标轴指示器，坐标轴触发有效
              type: "line", // 默认为直线，可选为：'line' | 'shadow'
            },
          },
          yAxis: {
            type: "value",
            name: "单位（万元）",
            splitLine: {
              lineStyle: {
                color: "#f0f0f0",
                type: "dashed",
              },
            },
          },
          series: [
            {
              name: "销售额",
              data: this.lineSS,
              type: "line",
              smooth: true,
              symbolSize: 1,
              lineStyle: {
                width: 3,
                color: "#ff6c6c",
              },
              areaStyle: {
                color: {
                  type: "linear",
                  x: 0,
                  y: 0,
                  x2: 0,
                  y2: 1,
                  colorStops: [
                    {
                      offset: 0,
                      color: "#ff6c6c", // 0% 处的颜色
                    },
                    {
                      offset: 1,
                      color: "#fff", // 100% 处的颜色
                    },
                  ],
                  //   label: {
                  //   show: true,
                  //   fontSize: 20,
                  //   fontWeight: "normal",
                  // },
                  global: false, // 缺省为 false
                },
              },
            },
          ],
        };
        myChart.setOption(option);
      }
    },
    getEchartData1() {
      const chart1 = this.$refs.chart1;
      if (chart1) {
        const myChart = this.$echarts.init(chart1);
        const option = {
          title: {
            text: "销售额分布",
            top: "2%",
            left: "42%",
          },
          xAxis: {
            type: "category",
            data: this.barXX,
            axisLine: {
              lineStyle: {
                type: "dashed",
                color: "#d4d4d6",
              },
            },
            // max:10 ,//关键第一句
            axisLabel: {
              color: "#000",
            },
          },
          tooltip: {
            show: true,
          },
          yAxis: {
            type: "value",
            name: "单位（万元）",
            splitLine: {
              lineStyle: {
                type: "dashed",
                color: "#f0f0f0",
              },
            },
          },

          series: [
            {
              name: "销售额",
              data: this.barSS,
              type: "bar",
              barWidth: "20",
              color: [
                {
                  type: "linear",
                  x: 0,
                  y: 0,
                  x2: 0,
                  y2: 1,
                  colorStops: [
                    {
                      offset: 0,
                      color: "#91b0ff", // 0% 处的颜色
                    },
                    {
                      offset: 1,
                      color: "#d5f2e7", // 100% 处的颜色
                    },
                  ],
                  global: false, // 缺省为 false
                },
              ],
            },
          ],
        };
        myChart.setOption(option);
      }
    },
    getEchartData2() {
      const chart2 = this.$refs.chart2;
      if (chart2) {
        const myChart = this.$echarts.init(chart2);
        const option = {
          tooltip: {
            trigger: "item",
            formatter: function (params) {
              return `${params.name}\n${params.value}%`;
            },
            rich: {
              a: { fontWeight: "bold" },
              b: { fontWeight: "normal" },
            },
          },
          // legend: {
          //   top: 'bottom',
          // },
          color: ["#85a7ff", "#99ebbd", "#ffb18b", "#c6ebff", "#becce6"],
          series: [
            {
              name: "合作商点位",
              type: "pie",
              radius: ["15%", "50%"],
              center: ["50%", "50%"],
              avoidLabelOverlap: false,
              roseType: "radius",
              itemStyle: {
                borderRadius: 30,
              },
              label: {
                show: true,
                position: "top",
                formatter: function (params) {
                  return `${params.name}\n${params.value}%`;
                },
                rich: {
                  a: { fontWeight: "bold" },
                  b: { fontWeight: "normal" },
                },
              },
              emphasis: {
                label: {
                  show: true,
                  fontSize: 20,
                  fontWeight: "normal",
                },
              },
              labelLine: {
                show: true,
                color: "#999",
              },
              data: this.pielist,
            },
          ],
        };
        myChart.setOption(option);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    width: 100%;
    height: 710px;
    margin: 30px;
    background-color: #f6f8f9;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
.upbox {
  display: flex;
  width: 100%;
  height: 60%;
  .upleftbox {
    width: 75%;
    padding: 5px;
    border-radius: 10px;
    .upleftup {
      height: 40%;
      .tasknum {
        height: 170px;
        background: #e9f3ff;
        background-image: url("../../layout/components/bg1.png"),
          url("http://likede2-admin.itheima.net/img/task.66b715b7.png");
        background-repeat: no-repeat, no-repeat;
        background-position: 0 0, calc(100% - 12px) 100%;
        padding: 10px;
        margin-right: 5px;
        padding-right: -10px;
        display: flex;
        -webkit-box-orient: vertical;
        -webkit-box-direction: normal;
        -ms-flex-direction: column;
        flex-direction: column;
        height: calc(20vh - 24px);
        min-height: 166px;
        border-radius: 20px;
        .header {
          height: 20%;
          display: flex;
          .title {
            -webkit-box-align: center;
            -ms-flex-align: center;
            align-items: center;
            -webkit-box-flex: 1;
            -ms-flex: 1;
            flex: 1;
            font-size: 16px;
            font-family: PingFangSC-Semibold, PingFang SC;
            font-weight: 600;
            color: #333;
            .sub-title {
              margin-left: 10px;
              font-size: 12px;
              font-family: PingFangSC-Regular, PingFang SC;
              font-weight: 400;
              color: #999;
            }
          }
        }
        .body {
          height: 80%;
          display: flex;
          justify-content: space-evenly;
          align-items: center;
          .stats {
            .item {
              display: flex;
              flex-direction: column;
              align-items: center;
              justify-content: space-evenly;
              .numcolor {
                font-size: 40px;
                font-weight: 700;
                color: #072074;
                margin-bottom: 10px;
                text-shadow: 2px 4px 5px #b9cfff;
              }
              .textcolor {
                margin-left: 25px;
                font-size: 12px;
                color: #a8aee6;
              }
            }
          }
        }
      }
    }
    .sellnum {
      height: 170px;
      background: #fbefe8
        url("http://likede2-admin.itheima.net/img/sale.606b0d8c.png") no-repeat
        calc(100% - 12px) 100%;
      padding: 10px;
      margin-left: 5px;
      padding-left: -5px;

      display: flex;
      -webkit-box-orient: vertical;
      -webkit-box-direction: normal;
      -ms-flex-direction: column;
      flex-direction: column;
      height: calc(20vh - 24px);
      min-height: 166px;
      border-radius: 20px;
      .header {
        height: 20%;
        display: flex;
        .title {
          -webkit-box-align: center;
          -ms-flex-align: center;
          align-items: center;
          -webkit-box-flex: 1;
          -ms-flex: 1;
          flex: 1;
          font-size: 16px;
          font-family: PingFangSC-Semibold, PingFang SC;
          font-weight: 600;
          color: #333;
          .sub-title {
            margin-left: 10px;
            font-size: 12px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #999;
          }
        }
      }
      .body {
        height: 80%;
        display: flex;
        justify-content: space-evenly;
        align-items: center;
        .stats {
          .item {
            display: flex;
            flex-direction: column;
            align-items: flex-start;
            justify-content: space-between;
            .numcolor {
              font-size: 40px;
              font-weight: 700;
              color: #ff5757;
              margin-bottom: 10px;
              text-shadow: 2px 4px 5px #fccdc4;
            }
            .textcolor {
              font-size: 12px;
              color: #de9690;
            }
          }
        }
      }
    }
    .upleftdown {
      margin: 5px;
      padding: 10px;
      border-radius: 10px;
      width: 100%;
      height: 60%;
      // background-color: rgb(72, 130, 181);
      .header {
        height: 10%;
        display: flex;
        justify-content: space-between;
        .title {
          -webkit-box-align: center;
          -ms-flex-align: center;
          align-items: center;
          -webkit-box-flex: 1;
          -ms-flex: 1;
          flex: 1;
          font-size: 16px;
          font-family: PingFangSC-Semibold, PingFang SC;
          font-weight: 600;
          color: #333;
          .sub-title {
            margin-left: 10px;
            font-size: 12px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #999;
          }
        }
        .title-right {
          display: flex;
          width: 120px;
          height: 30px;
          margin-top: -10px;
          border-radius: 10px;
          justify-content: space-evenly;
          align-items: center;
          background-color: #cfd9e3;
          span {
            width: 25px;
            height: 20px;
            border-radius: 5px;
            display: flex;
            justify-content: space-evenly;
            align-items: center;
            font-size: 14px;
            color: #999;
            background-color: rgb(231, 226, 226);
          }
          .active {
            font-weight: 700;
            font-size: 14px;
            color: #000;
            background-color: #fff;
          }
        }
      }
      .body {
        height: 90%;
        display: flex;
      }
    }
  }
  .uprightbox {
    padding: 10px;
    width: 25%;
    -webkit-font-smoothing: antialiased;
    .salerank {
      .header {
        height: 20%;
        display: flex;
        .title {
          -webkit-box-align: center;
          -ms-flex-align: center;
          align-items: center;
          -webkit-box-flex: 1;
          -ms-flex: 1;
          flex: 1;
          font-size: 16px;
          font-family: PingFangSC-Semibold, PingFang SC;
          font-weight: 600;
          color: #333;
          .sub-title {
            margin-left: 10px;
            font-size: 12px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #999;
          }
        }
      }
      .body {
        width: 100%;
        padding: 20px 20px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .el-row {
          margin-bottom: 15px;

          .count {
            color: rgb(118, 113, 113);
          }
        }
        .el-row .top {
          text-align: center;
          color: rgb(225, 86, 86);
          display: inline-block;
          width: 30px;
          height: 20px;
          margin-right: 5px;
        }
        .el-row:nth-child(1) .top {
          background: url("../../layout/components/金奖.png") no-repeat center
            center;
        }
        .el-row:nth-child(2) .top {
          background: url("../../layout/components/银奖.png") no-repeat center
            center;
        }
        .el-row:nth-child(3) .top {
          background: url("../../layout/components/铜奖.png") no-repeat center
            center;
        }
        .el-row:nth-child(n + 4) .top {
          background: url("../../layout/components/前十.png") no-repeat center
            center;
        }
      }
    }
  }
}
.downbox {
  width: 100%;
  height: 40%;
  border-radius: 10px;
  background-color: #f8fafd;

  .partner {
    height: 265px;
    margin: 5px;
    border-radius: 10px;
    background-color: #fff;
    .header {
      height: 20%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 10px;
      .title {
        -webkit-box-align: center;
        -ms-flex-align: center;
        align-items: center;
        -webkit-box-flex: 1;
        -ms-flex: 1;
        flex: 1;
        font-size: 16px;
        font-family: PingFangSC-Semibold, PingFang SC;
        font-weight: 600;
        color: #333;
      }
      .title-right {
        color: #6a8dff;
        margin-right: 10px;
      }
    }
    .body {
      height: 80%;
      display: flex;
      justify-content: space-between;
      .pie {
        width: 70%;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .table {
        width: 50%;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        .stats {
          display: flex;
          width: 120px;
          height: 200px;
          padding-left: 20px;
          border-radius: 10px;
          flex-direction: column;
          justify-content: space-evenly;
          align-items: flex-start;
          background-color: #f8f8f9;
          .item {
            display: flex;
            flex-direction: column;
            align-items: flex-start;
            justify-content: space-between;
            .numcolor {
              font-size: 35px;
              font-weight: 700;
              color: #072074;
              margin-bottom: 10px;
              text-shadow: 2px 4px 5px #b9cfff;
            }
            .textcolor {
              font-size: 16px;
              color: #333;
            }
          }
        }
      }
    }
  }
  .vm {
    height: 265px;
    margin: 5px;
    padding: 5px;
    border-radius: 10px;
    background-color: #fff;
    .header {
      height: 20%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 10px;
      .title {
        -webkit-box-align: center;
        -ms-flex-align: center;
        align-items: center;
        -webkit-box-flex: 1;
        -ms-flex: 1;
        flex: 1;
        font-size: 16px;
        font-family: PingFangSC-Semibold, PingFang SC;
        font-weight: 600;
        color: #333;
      }
      .title-right {
        color: #6a8dff;
        padding-right: 20px;
      }
    }
    .body {
      height: 80%;
      display: flex;
      flex-direction: column;
      justify-content: space-evenly;
      align-items: center;
      img {
        width: 123px;
        height: 96px;
      }
      span {
        font-size: 16px;
        color: #333;
      }
    }
  }
}
</style>
